/******************************************************************************
@file:  BloodRequirment.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/

package com.smartc.health;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BloodRequirment extends Activity implements OnItemClickListener {
	protected static final String TAG = "DoctersVisit";
	private ArrayList<Object> itemList;
	private ItemBean bean;

	ListView mItemListView;
	ListViewAdapter adapter;

	private HttpConnection mCityConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.doctor_visit);
		mCityConnection = new HttpConnection();
		mCityConnection.AddHandler(mResponse);
		TextView header = (TextView) findViewById(R.id.doc_header);
		header.setText("Blood Requirments");
	}

	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String query = getResources().getString(
				R.string.url_health_care_blood_requirement);
		String url = domain + query;
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onStart url:" + url);

		mCityConnection.setUrl(url);
		mCityConnection.clearParam();
		mCityConnection.Execute();
	}

	public static class ViewHolder {
		TextView Bloodgroup;
		TextView UnitRequired;
		TextView PatientName;
		TextView Location;
		TextView Hospital;
		// TextView PatientID;
		TextView Contact;
		TextView Date;
		TextView Time;
		// TextView Description;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		// private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			// mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.blood_requirement_1,
						null);

				holder.Bloodgroup = (TextView) convertView
						.findViewById(R.id.br_blood_group);
				holder.UnitRequired = (TextView) convertView
						.findViewById(R.id.br_unit_required);
				holder.PatientName = (TextView) convertView
						.findViewById(R.id.br_patient_name);
				holder.Location = (TextView) convertView
						.findViewById(R.id.br_location);
				holder.Hospital = (TextView) convertView
						.findViewById(R.id.br_hospital_name);
				/*
				 * holder.PatientID = (TextView) convertView
				 * .findViewById(R.id.br_patient_id);
				 */
				holder.Contact = (TextView) convertView
						.findViewById(R.id.br_contacts);
				holder.Date = (TextView) convertView.findViewById(R.id.br_date);
				holder.Time = (TextView) convertView.findViewById(R.id.br_time);
				/*
				 * holder.Description = (TextView) convertView
				 * .findViewById(R.id.br_description);
				 */

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);

			holder.Bloodgroup.setText(bean.nmBloodGroup);
			holder.UnitRequired.setText(bean.nrUnitRequired);

			holder.PatientName.setText(bean.nmPatient);
			holder.Location.setText(bean.nmLocation);
			holder.Hospital.setText(bean.nmHospital);
			/*
			 * holder.PatientID.setText(bean.idPatient);
			 */
			holder.Contact.setText(bean.nmContact);
			holder.Date.setText(bean.dtDate);
			holder.Time.setText(bean.tsTiming);
			// holder.Description.setText(bean.txtDescription);
			return convertView;
		}
	}

	public class ItemBean {
		String nmBloodGroup;
		String nrUnitRequired;
		String nmPatient;
		String nmLocation;
		String nmHospital;
		String idPatient;
		String nmContact;
		String dtDate;
		String tsTiming;
		String txtDescription;
	}

	public void AddObjectToList(String bldgr, String unitreq, String patient,
			String hospital, String location, String patientid, String contact,
			String date, String time, String desc) {

		bean = new ItemBean();
		bean.dtDate = date;
		bean.idPatient = patientid;
		bean.nmBloodGroup = bldgr;
		bean.nmContact = contact;
		bean.nmHospital = hospital;
		bean.nmLocation = location;
		bean.nmPatient = patient;
		bean.nrUnitRequired = unitreq;
		bean.tsTiming = time;
		bean.txtDescription = desc;
		itemList.add(bean);
	}

	public class ResponseHandler extends DefaultHandler {

		private boolean nmBloodGroup = false;
		private boolean nrUnitRequired = false;
		private boolean nmPatient = false;
		private boolean nmHospital = false;
		private boolean nmLocation = false;
		private boolean idPatient = false;
		private boolean nmContact = false;
		private boolean dtDate = false;
		private boolean tsTiming = false;
		private boolean txtDescription = false;

		public ArrayList<String> nmBloodGroup_List = new ArrayList<String>();
		public ArrayList<String> nrUnitRequired_List = new ArrayList<String>();
		public ArrayList<String> nmPatient_List = new ArrayList<String>();
		public ArrayList<String> nmHospital_List = new ArrayList<String>();
		public ArrayList<String> nmLocation_List = new ArrayList<String>();
		public ArrayList<String> idPatient_List = new ArrayList<String>();
		public ArrayList<String> nmContact_List = new ArrayList<String>();
		public ArrayList<String> dtDate_List = new ArrayList<String>();
		public ArrayList<String> tsTiming_List = new ArrayList<String>();
		public ArrayList<String> txtDescription_List = new ArrayList<String>();

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.nmBloodGroup) {
				nmBloodGroup_List.add(new String(ch, start, length));
			} else if (this.nrUnitRequired) {
				nrUnitRequired_List.add(new String(ch, start, length));
			} else if (this.nmPatient) {
				nmPatient_List.add(new String(ch, start, length));
			} else if (this.nmHospital) {
				nmHospital_List.add(new String(ch, start, length));
			} else if (this.nmLocation) {
				nmLocation_List.add(new String(ch, start, length));
			} else if (this.idPatient) {
				idPatient_List.add(new String(ch, start, length));
			} else if (this.nmContact) {
				nmContact_List.add(new String(ch, start, length));
			} else if (this.dtDate) {
				dtDate_List.add(new String(ch, start, length));
			} else if (this.tsTiming) {
				tsTiming_List.add(new String(ch, start, length));
			} else if (this.txtDescription) {
				txtDescription_List.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("Root")) {
				Message myMessage = new Message();
				myMessage.obj = "BLOOD";
				handler.sendMessage(myMessage);
			} else if (localName.equals("nmBloodGroup")) {
				this.nmBloodGroup = false;
			} else if (localName.equals("nrUnitRequired")) {
				this.nrUnitRequired = false;
			} else if (localName.equals("nmPatient")) {
				this.nmPatient = false;
			} else if (localName.equals("nmHospital")) {
				this.nmHospital = false;
			} else if (localName.equals("nmLocation")) {
				this.nmLocation = false;
			} else if (localName.equals("idPatient")) {
				this.idPatient = false;
			} else if (localName.equals("nmContact")) {
				this.nmContact = false;
			} else if (localName.equals("dtDate")) {
				this.dtDate = false;
			} else if (localName.equals("tsTiming")) {
				this.tsTiming = false;
			} else if (localName.equals("txtDescription")) {
				this.txtDescription = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("Root")) {
				// this.TradeProducts = true;
				nmBloodGroup_List.clear();
				nrUnitRequired_List.clear();
				nmPatient_List.clear();
				nmHospital_List.clear();
				nmLocation_List.clear();

				idPatient_List.clear();
				nmContact_List.clear();
				dtDate_List.clear();
				tsTiming_List.clear();
				txtDescription_List.clear();
			} else if (localName.equals("nmBloodGroup")) {
				this.nmBloodGroup = true;
			} else if (localName.equals("nrUnitRequired")) {
				this.nrUnitRequired = true;
			} else if (localName.equals("nmPatient")) {
				this.nmPatient = true;
			} else if (localName.equals("nmHospital")) {
				this.nmHospital = true;
			} else if (localName.equals("nmLocation")) {
				this.nmLocation = true;
			} else if (localName.equals("idPatient")) {
				this.idPatient = true;
			} else if (localName.equals("nmContact")) {
				this.nmContact = true;
			} else if (localName.equals("dtDate")) {
				this.dtDate = true;
			} else if (localName.equals("tsTiming")) {
				this.tsTiming = true;
			} else if (localName.equals("txtDescription")) {
				this.txtDescription = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg != null && loginmsg.equals("BLOOD")) {
				itemList = new ArrayList<Object>();
				if (AccountLogin.DEBUG)
					Log.i(TAG, "handleMessage size:"
							+ mResponse.nmBloodGroup_List.size());
				for (int i = 0; i < mResponse.nmBloodGroup_List.size(); i++) {
					AddObjectToList("Blood Group: "
							+ mResponse.nmBloodGroup_List.get(i),
							"Units Required: "
									+ mResponse.nrUnitRequired_List.get(i),
							"Patient Name: " + mResponse.nmPatient_List.get(i),
							"Hospital Name: "
									+ mResponse.nmHospital_List.get(i),
							"Location: " + mResponse.nmLocation_List.get(i),
							"Patient Id: " + mResponse.idPatient_List.get(i),
							"Contacts: " + mResponse.nmContact_List.get(i),
							"Date: " + mResponse.dtDate_List.get(i), "Time: "
									+ mResponse.tsTiming_List.get(i),
							"Description: "
									+ mResponse.txtDescription_List.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.doctor_list);
				adapter = new ListViewAdapter(BloodRequirment.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
				mItemListView.setOnItemClickListener(BloodRequirment.this);
			}

		}
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onItemClick position" + position);
		/*
		 * Intent intent = new Intent(this,DoctorsRegistration.class);
		 * intent.putExtra("docname", mResponse.mDoctorName_List.get(position));
		 * intent.putExtra("specalist",
		 * mResponse.mDoctorDesc_List.get(position)); intent.putExtra("address",
		 * mResponse.mContact_List.get(position)); intent.putExtra("time",
		 * mResponse.mTime_List.get(position)); startActivity(intent);
		 */
	}

}
